package{
public dynamic class Json extends Object{
	//JSON with uppercase is already defined in as3, btw
	/* prototype testing data :
	{ "data":   {"hehe":[   123, [{"name" : "my name is Kristo " } ," lohh"  ] ]  } ,
	"data2bro": "my love"}
		trace(json[id].data.hehe[0]);
		trace(json[id].data.hehe[1][0].name);
		trace(json[id].data.hehe[1][1]);
		trace(json[id].data2bro);
	*/
	public function Json(str:String){
		//creates as3 Object type
		//first, check the [] {} count
		var count1:Number, count2:Number;
		count1 = su.count(str,"{");
		count2 = su.count(str,"}");
		if(count1 != count2){
			throw new Error("{ and } doesn't match");
			return;
		}
		count1 = su.count(str,"[");
		count2 = su.count(str,"]");
		if(count1 != count2){
			throw new Error("[ and ] doesn't match");
			return;
		}
		//cleaning
		str = su.trim(str);
		str = str.substring(1,str.length-1);	//remove { } at start/end
		str = su.trim(str);
		trace("Json data comes : "+str);
		
		//find special : { for json object, [ for array
		while(str.length > 0){
			//find the field name first
			str = su.trim(str);
			if(str.charAt(0) == ","){
				str = str.substring(1);
			}
			str = su.trim(str);
			var iof:Number = str.indexOf("\"",2);
			trace("current : "+str+"\n---");
			var field:String = su.fixStr(str.substring(1,iof));
			str = str.substring(iof+1);
			//find the data
			var found:Boolean = false;
			var content:Object;
			for(var i=0;i<str.length;i++){
				if(str.charAt(i) == ":"){
					found = true;
				}
				if(found && str.charAt(i) == "\""){
					//the content is string/number
					iof = str.indexOf("\"",i+1);
					content = str.substring(i+1,iof);
					this[field] = su.getData(content);
					trace("Adding (string/number) "+field+" : "+this[field]);
					str = str.substring(iof+1);
					break;
				}
				if(found && str.charAt(i) == "["){
					//the content is array
					//find the closing bracket
					var cl:Number = su.findClosing(str,i+1,"[","]");
					if(cl == -1){
						throw new Error("Could not find ] closing in "+str);
						return;
					}else{
						var arrS:String = str.substring(i+1,cl);
						this[field] = new JsonArray(arrS);
						trace("Adding (array) "+field+" : "+this[field]);
						str = str.substring(cl+1);
						break;
					}
				}
				if(found && str.charAt(i) == "{"){
					//nested json
					var cl:Number = su.findClosing(str,i+1,"{","}");
					if(cl == -1){
						throw new Error("Could not find } closing in "+str);
						return;
					}else{
						var jsonStr:String = str.substring(i,cl+1);
						trace(jsonStr);
						this[field] = new Json(jsonStr);
						trace("Adding (json) "+field+" : "+this[field].print());
						str = str.substring(cl+1);
						break;
					}
				}
			}
			trace(str);
		}
		trace("Result data : "+print());
		try{
			//testing purposes
			trace("Result checking = "+this.data[0].subtitle);
			trace("Result checking = "+this.data[2].content[1]);
		}catch(e){}
	}
	function get su(){
		return StringU;
	}
	function print():String{
		var str:String = "";
		for(var i in this){
			str += i+":"+this[i]+",";
		}
		str = str.substring(0,str.length-2);
		return str;
	}

}
}